package com.zzl.alligator.management.system.controller;

import com.zzl.alligator.common.enums.BusinessTypeEnum;
import com.zzl.alligator.common.util.ExcelUtils;
import com.zzl.alligator.framework.aspectj.annotation.Log;
import com.zzl.alligator.framework.base.controller.BaseController;
import com.zzl.alligator.framework.support.web.vo.JsonResult;
import com.zzl.alligator.management.system.domain.SysUserNotice;
import com.zzl.alligator.management.system.service.ISysUserNoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 用户通知表(SysUserNotice)控制层
 *
 * @author zhouziliang
 * @since 2020-07-15
 */
@RestController
@RequestMapping("/system/userNotice")
public class SysUserNoticeController extends BaseController {
    
    /**
     * 服务对象
     */
    @Autowired
    private ISysUserNoticeService sysUserNoticeService;

    /**
     * 分页查询所有数据
     *
     * @param sysUserNotice 查询实体
     * @return 所有数据
     */
    @GetMapping
    public JsonResult findList(SysUserNotice sysUserNotice) {
        startPage();
        List<SysUserNotice> sysUserNoticeList = sysUserNoticeService.findSysUserNoticeList(sysUserNotice);
        return JsonResult.success(getDataTable(sysUserNoticeList));
    }



    /**
     * 新增数据
     *
     * @param sysUserNotice 实体对象
     * @return 新增结果
     */
    @PostMapping
    @Log(title = "新增用户通知表", businessType = BusinessTypeEnum.INSERT)
    public JsonResult add(@RequestBody SysUserNotice sysUserNotice) {
        return toAjax(this.sysUserNoticeService.insertSysUserNotice(sysUserNotice));
    }

    /**
     * 修改数据
     *
     * @param sysUserNotice 实体对象
     * @return 修改结果
     */
    @PutMapping
    @Log(title = "更新用户通知表", businessType = BusinessTypeEnum.UPDATE)
    public JsonResult update(@RequestBody SysUserNotice sysUserNotice) {
        return toAjax(this.sysUserNoticeService.updateSysUserNotice(sysUserNotice));
    }

    /**
     * 所有已读取
     *
     * @param userId 用户ID
     * @return 结果
     */
    @PutMapping("/readAll/{userId}")
    @Log(title = "用户读取所有通知", businessType = BusinessTypeEnum.UPDATE)
    public JsonResult readAll(@PathVariable("userId") Long userId) {
        sysUserNoticeService.readAll(userId);
        return JsonResult.success();
    }

    /**
     * 用户读取通知
     *
     * @param userId 用户ID
     * @param noticeId 通知ID
     * @return 结果
     */
    @PutMapping("/read/{userId}/{noticeId}")
    public JsonResult updateNoticeRead(@PathVariable("userId")  Long userId,
                                       @PathVariable("noticeId") Integer noticeId) {
        return toAjax(sysUserNoticeService.updateNoticeRead(userId, noticeId));

    }

    /**
     * 删除数据
     *
     * @param userId 主键
     * @return 删除结果
     */
    @DeleteMapping("/{userId}")
    @Log(title = "删除用户通知表", businessType = BusinessTypeEnum.DELETE)
    public JsonResult delete(@PathVariable Long userId) {
        return toAjax(this.sysUserNoticeService.deleteById(userId));
    }
    
    /**
     * 导出数据
     * 
     * @param sysUserNotice 条件
     * @return 导出文件路径
     */
    @GetMapping("/export")
    @Log(title = "导出用户通知表", businessType = BusinessTypeEnum.EXPORT)
    public JsonResult export(SysUserNotice sysUserNotice) {
        List<SysUserNotice> list = sysUserNoticeService.findSysUserNoticeList(sysUserNotice);
        ExcelUtils<SysUserNotice> util = new ExcelUtils<>(SysUserNotice.class);
        return util.exportExcel(list, "用户通知表数据");
    }
}